# -*-coding:utf-8-*-

import pyrestful.rest
from pyrestful import mediatypes
from pyrestful.rest import get, post, put, delete
from pymongo.collection import ReturnDocument
from utils.auth import authenticated
import time, json
from datetime import datetime, timedelta
import handlers.base


class DealersHandler(handlers.base.BaseHandler):
    def __init__(self, application, request, **kwargs):
        super(DealersHandler, self).__init__(application, request, **kwargs)
        self.dealers_dao = self.settings['daos']['dealers']
        self.products_dao = self.settings['daos']['products']
        self.users_dao = self.settings['daos']['users']

    def check_permissions(self, permissions):
        return True

    @authenticated
    @get(_path='/platform/api/dealers', _produces=mediatypes.APPLICATION_JSON)
    def get_dealer_list(self):
        result = self.dealers_dao.get_dealer_list(self.user_id)
        self.response_success(result)

    @authenticated
    @post(_path='/platform/api/dealers', _produces=mediatypes.APPLICATION_JSON)
    def create_dealer(self):
        request = json.loads(self.request.body)
        phone = request['phone']
        user = self.users_dao.get_user_by_phone_no(self.app, phone)
        if user is None:
            self.response_error('user_not_exists')
            return
        dealer_id = user.id_str
        if self.dealers_dao.get_dealer_by_id(dealer_id) is not None:
            self.response_error('user_already_exists')
            return
        request['dealer_id'] = dealer_id
        result = self.dealers_dao.create_dealer(self.user_id, request)
        self.users_dao.update_user_with_dealer_id_vendor_id(self.app, result['dealer_id'], self.user_id)
        self.response_success(result)

    @authenticated
    @get(_path='/platform/api/dealers/{did}', _types=[str], _produces=mediatypes.APPLICATION_JSON)
    def get_dealer(self, did):
        result = self.dealers_dao.get_dealer(self.user_id, did)
        self.response_success(result)

    @authenticated
    @put(_path='/platform/api/dealers/{did}', _types=[str], _produces=mediatypes.APPLICATION_JSON)
    def update_dealer(self, did):
        request = json.loads(self.request.body)
        result = self.dealers_dao.update_dealer(self.user_id, did, request)
        self.response_success(result)

    @authenticated
    @delete(_path='/platform/api/dealers/{did}', _types=[str], _produces=mediatypes.APPLICATION_JSON)
    def delete_dealer(self, did):
        result = self.dealers_dao.delete_dealer(self.user_id, did)
        self.response_success(result)

    '''
    products of dealer
    '''

    @authenticated
    @get(_path='/platform/api/dealers/{did}/products', _types=[str], _produces=mediatypes.APPLICATION_JSON)
    def get_dealer_product_list(self, did):
        result = self.dealers_dao.get_dealer_product_list(self.user_id, did)
        if result is None:
            result = []
        self.response_success(result)

    @authenticated
    @post(_path='/platform/api/dealers/{did}/products', _types=[str], _produces=mediatypes.APPLICATION_JSON)
    def add_dealer_product(self, did):
        request = json.loads(self.request.body)
        pid = request['product_id']
        p = yield self.products_dao.get_record_by_id(pid)
        if p is None:
            raise Exception('product not exists')
        result = self.dealers_dao.add_dealer_product(self.user_id, did, request)
        self.response_success(result)

    @authenticated
    @put(_path='/platform/api/dealers/{did}/products/{pid}', _types=[str, str], _produces=mediatypes.APPLICATION_JSON)
    def update_dealer_product(self, did, pid):
        if self.products_dao.get_record_by_id(pid) is None:
            raise Exception('product not exists')
        request = json.loads(self.request.body)
        result = self.dealers_dao.update_dealer_product(self.user_id, did, pid, request)
        self.response_success(result)

    @authenticated
    @get(_path='/platform/api/dealers/{did}/products/{pid}', _types=[str, str], _produces=mediatypes.APPLICATION_JSON)
    def get_dealer_product(self, did, pid):
        if self.products_dao.get_record_by_id(pid) is None:
            raise Exception('product not exists')
        result = self.dealers_dao.get_dealer_product(self.user_id, did, pid)
        self.response_success(result)


    @delete(_path='/platform/api/dealers/{did}/products/{pid}', _types=[str, str],
            _produces=mediatypes.APPLICATION_JSON)
    def delete_dealer_product(self, did, pid):
        if self.products_dao.get_record_by_id(pid) is None:
            raise Exception('product not exists')
        result = self.dealers_dao.delete_dealer_product(self.user_id, pid, did)
        self.response_success(result)

    '''
        members of dealer
    '''

    @authenticated
    @get(_path='/platform/api/dealers/{did}/members', _types=[str], _produces=mediatypes.APPLICATION_JSON)
    def get_dealer_member_list(self, did):
        result = self.users_dao.get_dealer_member_list(self.app, did, **self.request.query_arguments)
        self.response_success(result)
